﻿#include "Khazard.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../Settings.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Khazard::Khazard(MonsterInfo *info) : MonsterObject(info)
    {
        PullTime = getEnvir()->getTime();
    }

    bool Khazard::getCanPull() const
    {
        return Range && getEnvir()->getTime() >= PullTime;
    }

    bool Khazard::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 4 || y > 4)
        {
            return false;
        }

        bool targetinrange = (x == 0) || (y == 0) || (x == y);
        if (!targetinrange)
        {
            return false;
        }

        Range = (x > 1 || y > 1) ? true : false;

        return (!Range || getCanPull()) ? true : false;
    }

    void Khazard::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (Range && getCanPull())
        {
            PullAttack();
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            ActionTime = getEnvir()->getTime() + 300;
            AttackTime = getEnvir()->getTime() + AttackSpeed;
            PullTime = getEnvir()->getTime() + 5000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            if (!Range)
            {
                MonsterObject::Attack();
            }
        }
    }

    void Khazard::PullAttack()
    {
        for (int i = 1; i <= 4; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), i);

            if (target == getTarget()->getCurrentLocation())
            {
                if (getEnvir()->Random->Next(Settings::MagicResistWeight) < getTarget()->Stats[Stat::MagicResist])
                {
                    continue;
                }
                MirDirection pushdir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());
                getTarget()->Pushed(this, pushdir, i);
            }
        }
    }

    void Khazard::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            if (getTarget()->Dead)
            {
                FindTarget();
            }

            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
